using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Appcontext.CommandHandler
{
	/// <summary>
	/// 根据ID获取挂号单模板查询处理器
	/// </summary>
	public class GetRegistrationSlipTemplateByIdQueryHandler : IRequestHandler<GetRegistrationSlipTemplateByIdQuery, RegistrationSlipTemplateDto>
	{
		private readonly IBaseRepository<RegistrationSlipTemplate> _repository;

		public GetRegistrationSlipTemplateByIdQueryHandler(IBaseRepository<RegistrationSlipTemplate> repository)
		{
			_repository = repository;
		}

		public async Task<RegistrationSlipTemplateDto> Handle(GetRegistrationSlipTemplateByIdQuery request, CancellationToken cancellationToken)
		{
			var entity = await _repository.GetModel(x => x.TemplateId == request.TemplateId && !x.IsDeleted);
			
			if (entity == null)
			{
				throw new Exception("模板不存在");
			}

			return new RegistrationSlipTemplateDto
			{
				TemplateId = entity.TemplateId,
				TemplateName = entity.TemplateName,
				Description = entity.Description,
				IsActive = entity.IsActive,
				IsDefault = entity.IsDefault,
				PriorityRules = entity.PriorityRules,
				ConfiguredFields = entity.ConfiguredFields,
				CreatedAt = entity.CreatedAt,
				UpdatedAt = entity.UpdatedAt
			};
		}
	}
}
